var gSlideChoice = "-1";

// move slider by clicking
function moveSlider(evt, whichSlider, minPos, maxPos) { 

        var thumbWidth=8;
        var sliderLength = maxPos - minPos;

        // compute the relative location of mouse from 0 to 100
	var loc = (evt.getClientX() - minPos) / sliderLength * 100;
	var loc1 = svgDocument.getElementById("slider" + whichSlider + "_thumb1").getAttribute( "realLoc" );
	var loc2 = svgDocument.getElementById("slider" + whichSlider + "_thumb2").getAttribute( "realLoc" );

        // determine which slider is the closest one.
        var aDiff1 =  loc - loc1 ;
        if(aDiff1<0) {
           aDiff1 = -aDiff1;
        }

        var aDiff2 =  loc - loc2 ;
        if(aDiff2<0) {
           aDiff2 = -aDiff2;
        }

        // move to the point
        var thumb;
        var textbox;
	var pos = evt.getClientX() - minPos;
        if(aDiff1<=aDiff2) {
	   thumb = svgDocument.getElementById("slider" + whichSlider + "_thumb1");
	   thumb.setAttribute( "transform", "translate(" + pos + " 0)" );

	   textbox = svgDocument.getElementById("sliderText" + whichSlider + "_thumb1");
        }
        else {
	   thumb = svgDocument.getElementById("slider" + whichSlider + "_thumb2");
	   textbox = svgDocument.getElementById("sliderText" + whichSlider + "_thumb2");
 
           if(loc>100-thumbWidth) {
              pos = pos - thumbWidth;
	      thumb.setAttribute( "transform", "translate(" + pos + " 0)" );
           }
	   else {
	      thumb.setAttribute( "transform", "translate(" + pos + " 0)" );
           }
        }


        // adjust the extreme parts
        if(loc<1)  loc = 0;
        if(loc>99) loc = 100;

	// printing the location of slider	
        loc = "" + loc + "";
 	textbox.getFirstChild().setNodeValue( loc.substring(0,4) + " %");

        thumb.setAttribute( "realLoc", loc );



        // change the color of disselected genes and rearange the GO terms
        updateChange();

}

// make slider change update the gene ontology and gene color
function updateChange() 
{
        // get the value of selected ranges of sliders
	var locThumbX1 = svgDocument.getElementById("sliderX_thumb1").getAttribute("realLoc");
	var locThumbX2 = svgDocument.getElementById("sliderX_thumb2").getAttribute("realLoc");
	var locThumbY1 = svgDocument.getElementById("sliderY_thumb1").getAttribute("realLoc");
	var locThumbY2 = svgDocument.getElementById("sliderY_thumb2").getAttribute("realLoc");
	var locThumbZ1 = svgDocument.getElementById("sliderZ_thumb1").getAttribute("realLoc");
	var locThumbZ2 = svgDocument.getElementById("sliderZ_thumb2").getAttribute("realLoc");
	

	var selectedGeneNo = 0;
        var ii;


        // found out the height of input_gene
	var inputSVG = svgDocument.getElementById("GO_line_" + gInputGene);
        var inputHeight = inputSVG.getAttribute( "height" );
        var xHeight = gGOBoxY + parseInt ( inputHeight ); //inputHeight;


	for(ii=0; ii<gGeneNo; ii++) {		    
			var gene = gGeneList[ii]["name"];
			var geneX = gGeneList[ii]["x"];
			var geneY = gGeneList[ii]["y"];
			var geneZ = gGeneList[ii]["z"];
						
		        var geneObj = svgDocument.getElementById(gene);
			var goSVG = svgDocument.getElementById("GO_line_" + gene);

			if(geneX>=locThumbX1 &&
			   geneX<=locThumbX2 &&
			   geneY>=locThumbY1 &&
			   geneY<=locThumbY2 &&
			   geneZ>=locThumbZ1 &&
			   geneZ<=locThumbZ2 ) {
			        geneObj.setAttribute( "fill", "red" );

                                // input gene drawn always	
	                        if(gene != gInputGene) { // && xHeight < gGOBoxHeight) {
			        	selectedGeneNo++;	

					// set GO annotation for selected points
                                	goSVG.setAttribute( "y", xHeight);

                                        // update the xHeight to the new position
                                        xHeight = xHeight + parseInt( goSVG.getAttribute("height") );
                                }
                               
			}
			else {
				geneObj.setAttribute( "fill", "white" );	

                                // input gene drawn always	
	                        if(gene != gInputGene) {
                                	goSVG.setAttribute( "y", -200);
                                }
			}
	}
	
}

// move slider by dragging mouse, but doesn't work
function startDrag(whichSlider) 
{	
	endDrag();
	gSlideChoice = whichSlider;
}

function endDrag() 
{
	gSlideChoice = "-1";
}

function doDrag(evt, whichSlider, minPos, maxPos)
{
	if(gSlideChoice=="-1" || gSlideChoice!=whichSlider) {
		return;
	}

        var sliderLength = maxPos - minPos;

	var obj = evt.getTarget();
	var pos = evt.getClientX() - minPos - 10;
	var loc = pos/sliderLength * 100;
	
	if(pos < 1) { pos=0; }
	if(pos > sliderLength-1) { pos=sliderLength; }

	// moving a slider thumb
	//obj = svgDocument.getElementById("slider" + whichSlider);
	obj.setAttribute( "transform", "translate(" + pos + " 0)" );
	obj.setAttribute( "realLoc", loc );
	
	// printing the location of slider	
	var textbox = svgDocument.getElementById("sliderText" + whichSlider);
	// printing the location of slider	
        loc = "" + loc + "";
        if(loc.length>4) {
             loc = loc.substring(0, 4);
        }
 	textbox.getFirstChild().setNodeValue(loc + " Haha %");

        // update the changes to the genes and gene ontologies
        updateChange();
}
